package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.bo.AdvertiseBO;
import com.junzhiit.nengji.cms.service.AdvertiseService;
import com.junzhiit.nengji.cms.web.exception.AdvertiseNotFoundException;
import com.junzhiit.nengji.cms.web.util.FileUtil;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.vo.AdvertiseAddVO;
import com.junzhiit.nengji.cms.web.vo.AdvertiseEditVO;
import com.junzhiit.nengji.cms.web.vo.AdvertiseVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.AdvertiseDAO;
import com.junzhiit.nengji.model.Advertise;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class AdvertiseServiceImpl extends BaseServiceImpl implements AdvertiseService {

	@Autowired
	private AdvertiseDAO advertiseDAO;
	
	@Value("${upload.temp}")
	private String uploadPath;

	@Value("${upload.mall.advertise}")
	private String advertisePath;

	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, AdvertiseVO advertiseVO) {
		Advertise advertise = new Advertise();
		BeanUtils.copyProperties(advertiseVO, advertise);
		List<AdvertiseBO> list = advertiseDAO.findAll(dtp.getStart(), dtp.getLength(), advertise);

		List<AdvertiseVO> listVO = new ArrayList<AdvertiseVO>();
		for (AdvertiseBO advertiseBO : list) {
			AdvertiseVO vo = new AdvertiseVO();
			BeanUtils.copyProperties(advertiseBO, vo);
			listVO.add(vo);
		}
		
		int totalCount = advertiseDAO.getTotalCount(advertise);
		return ViewObjectUtil.dataTables(listVO, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id) {
		ResultVO resultVO = new ResultVO();
		AdvertiseBO advertiseBO = advertiseDAO.getAdvertiseById(id);
		if (advertiseBO == null) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}
		AdvertiseVO advertiseVO = new AdvertiseVO();
		BeanUtils.copyProperties(advertiseBO, advertiseVO);
		resultVO.setData(advertiseVO);
		return resultVO;
	}

	@Override
	public ResultVO add(AdvertiseAddVO advertiseAddVO) {
		Advertise advertise = new Advertise();
		BeanUtils.copyProperties(advertiseAddVO, advertise);
		advertise.setStatus(Status.ACTIVE.toIntStatus());
		ResultVO resultVO = new ResultVO();
		Advertise param =advertiseDAO.insert(advertise);
		AdvertiseVO advertiseVO = new AdvertiseVO();
		BeanUtils.copyProperties(param, advertiseVO);
		resultVO.setData(advertiseVO);
		FileUtil.moveFile(uploadPath + advertiseVO.getImgUrl(), advertisePath);
		boolean is = AliyunOSSClientUtil.moveFile(backetName,advertiseVO.getImgUrl(), advertisePath, uploadPath);
		if(is){
			AliyunOSSClientUtil.removeFile(backetName, uploadPath+advertiseVO.getImgUrl());
		}
		return resultVO;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO resultVO = new ResultVO();
		try {
			resultVO.setData(advertiseDAO.updateToDelete(id));
		} catch (Exception e) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}
		return resultVO;
	}

	@Override
	@Transactional
	public ResultVO edit(AdvertiseEditVO advertiseEditVO) {
		ResultVO resultVO = new ResultVO();
		Advertise advertise = advertiseDAO.query(advertiseEditVO.getId());
		
		if (advertise == null) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}
		if (StringUtils.isNotBlank(advertiseEditVO.getImgUrl())
				&& !advertiseEditVO.getImgUrl().equals(advertise.getImgUrl())) {
			//FileUtil.moveFile(uploadPath + advertiseEditVO.getImgUrl(), advertisePath);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,advertiseEditVO.getImgUrl(), advertisePath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+advertiseEditVO.getImgUrl());
			}
		}
		
		BeanUtils.copyProperties(advertiseEditVO, advertise);
		advertise.setUpdatedTime(new Date());
		resultVO.setData(advertiseDAO.update(advertise));
		return resultVO;
	}

	@Override
	public ResultVO toTop(String id) {
		ResultVO resultVO = new ResultVO();
		Advertise advertise = advertiseDAO.query(id);
		if (advertise == null) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}
		advertise.setSort(advertiseDAO.getMaxSort() + 1);
		advertise.setUpdatedTime(new Date());
		resultVO.setData(advertiseDAO.update(advertise));
		return resultVO;
	}

	@Override
	public ResultVO changeStatus(String id) {
		ResultVO resultVO = new ResultVO();
		Advertise advertise = advertiseDAO.query(id);
		if (advertise == null) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}
		if (advertise.getStatus() == Status.INACTIVE.toIntStatus()) {
			advertise.setStatus(Status.ACTIVE.toIntStatus());
		} else {
			advertise.setStatus(Status.INACTIVE.toIntStatus());
		}
		advertise.setUpdatedTime(new Date());
		resultVO.setData(advertiseDAO.update(advertise));
		return resultVO;
	}

	@Override
	public ResultVO delAdvertisePic(String id) {
		ResultVO result = new ResultVO();
		
		Advertise advertise = advertiseDAO.query(id);
		if (advertise == null) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}
		
		//boolean flag = FileUtil.removeFile(advertisePath + advertise.getImgUrl());
		boolean flag = AliyunOSSClientUtil.removeFile(backetName, advertisePath+advertise.getImgUrl());

		advertise.setImgUrl(null);
		advertise.setUpdatedTime(new Date());
		advertiseDAO.update(advertise, true);

		result.setData(flag);
		return result;
	}

}
